package com.cupcorn.lib.display;

import org.andengine.entity.Entity;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.TextOptions;
import org.andengine.util.HorizontalAlign;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameText;
import com.cupcorn.lib.game.element.GameElementText;

public class DisplayText
{
	GameEngine			engine;
	GameElementText[]	gameElementText;
	DisplayFont			font;
	public GameText[]	text;
	
	public DisplayText(GameEngine engine, DisplayFont font, GameElementText[] gameElementText)
	{
		this.font = font;
		this.engine = engine;
		this.gameElementText = gameElementText;
		this.text = new GameText[gameElementText.length];
	}
	
	public GameText newText(int index)
	{
		return newText(index, engine.getEngine().getScene());
	}
	
	public GameText newText(int index, Entity parent)
	{
		GameText text = text(index);
		parent.attachChild(text);
		return text;
	}
	
	public GameText text(int index)
	{
		GameText text;
		
		if (gameElementText[index].charactersMaximum != 0)
			text =
				new GameText(0, 0, font.font[gameElementText[index].indexFont], gameElementText[index].content, gameElementText[index].charactersMaximum,
					engine.getVertexBufferObjectManager());
		else if (gameElementText[index].useWrap)
			text =
				new GameText(0, 0, font.font[gameElementText[index].indexFont], gameElementText[index].content, new TextOptions(AutoWrap.WORDS,
					GameEngine.CAMERA_WIDTH * gameElementText[index].wrapWidth, HorizontalAlign.CENTER), engine.getVertexBufferObjectManager());
		else
			text = new GameText(0, 0, font.font[gameElementText[index].indexFont], gameElementText[index].content, engine.getVertexBufferObjectManager());
		
		float x = Utils.getPosX(gameElementText[index].x, text.getWidth(), gameElementText[index].anchor, gameElementText[index].ratio);
		float y = Utils.getPosY(gameElementText[index].y, text.getHeight(), gameElementText[index].anchor, gameElementText[index].ratio);
		
		text.setPosition(x, y);
		this.text[index] = text;
		return text;
	}
}
